"""
    Author:
    Date: 2018-12-29

    微信管理后台-照片审核
"""

import json
from django.shortcuts import render

from utils import ajax

from entities.consts import *
from entities import photo_mgr
from entities import user_mgr
from entities import vchat_api_mgr
from utils.log import Log

from .. import helpers
from .. helpers import gma_login_required


@gma_login_required
def index(request):
    """ 照片审核首页 """
    context = helpers.build_context(request)
    context['waiting_auth_photo_count'] = photo_mgr.get_photo_count_by_auth_status(PHOTO_AUTH_STATUS_WAITING, request)
    context['unauthorized_photo_count'] = photo_mgr.get_photo_count_by_auth_status(PHOTO_AUTH_STATUS_REJECT, request)
    context['authorized_photo_count'] = photo_mgr.get_photo_count_by_auth_status(PHOTO_AUTH_STATUS_AGREE, request)
    return render(request, 'gma/photo/photo_home.html', context)


@gma_login_required
def waiting_photos(request):
    """ 待审的照片列表 """
    context = helpers.build_context(request)
    context['photos'] = photo_mgr.get_photo_list_by_auth_status(PHOTO_AUTH_STATUS_WAITING, 100, request)
    return render(request, 'gma/photo/photo_list.html', context)


@gma_login_required
def rejected_photos(request):
    """ 审核被拒的照片列表 """
    context = helpers.build_context(request)
    context['photos'] = photo_mgr.get_photo_list_by_auth_status(PHOTO_AUTH_STATUS_REJECT, 100, request)
    return render(request, 'gma/photo/photo_list.html', context)


@gma_login_required
def agreed_photos(request):
    """ 审核通过的照片列表 """
    context = helpers.build_context(request)
    context['photos'] = photo_mgr.get_photo_list_by_auth_status(PHOTO_AUTH_STATUS_AGREE, 100, request)
    return render(request, 'gma/photo/photo_list.html', context)


@gma_login_required
def auth_photo(request, pid):
    """ 照片的审核页面 """
    photo = photo_mgr.get_photo_info(pid)

    context = helpers.build_context(request)
    context['user'] = user_mgr.get_user_detail(photo['uid'])
    context['photo'] = photo
    context['images'] = [photo['show_picture_url']]
    return render(request, 'gma/photo/auth_photo.html', context)


@gma_login_required
def api_agree_photo(request):
    """ 同意照片上架 """
    vid = request.POST['photo_id'].strip()
    photo = photo_mgr.get_photo_info(vid)
    if not photo:
        return ajax.error_response('无效的照片id!')

    # 更改审核状态
    success = photo_mgr.update_photo_auth_status(vid, PHOTO_AUTH_STATUS_AGREE)
    if not success:
        Log.error(f'Agree photo status failed: vid={vid}')
    else:
        Log.info(f'Agree photo status ok: vid={vid}')

    return ajax.success_response()


@gma_login_required
def api_reject_photo(request):
    """ 拒绝照片上架 """
    vid = request.POST['photo_id'].strip()
    photo = photo_mgr.get_photo_info(vid)
    if not photo:
        return ajax.error_response('无效的照片id!')

    success = photo_mgr.update_photo_auth_status(vid, PHOTO_AUTH_STATUS_REJECT)
    if not success:
        Log.error(f'Reject photo status failed: vid={vid}')
    else:
        Log.info(f'Reject photo status ok: vid={vid}')

    data = {}
    return ajax.success_response(data)


if __name__ == '__main__':
    print('ok')
